package cn.cc.order.service.serviceImpl;

import cn.cc.baycar.service.bo.AddPayCarDO;
import cn.cc.common.beanCopy.utils.BeanCopierUtil;
import cn.cc.common.contants.RedisKeyConstant;
import cn.cc.common.redis.RedisCommon;
import cn.cc.common.result.utils.RestResult;
import cn.cc.common.result.utils.RestResultEmum;
import cn.cc.order.service.bo.Order;
import cn.cc.order.service.OrderService;
import cn.cc.order.service.domain.PayCar;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: smail-mi-cloud
 * @description: 订单主体功能实现
 * @author: Mr.WuChao
 * @create: 2021-07-26 10:25
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisCommon redisCommon;

    @Override
    public RestResult<Void> addOrder(Order order) {
        //购物车接口  sku+spu  集合
        Map<String, AddPayCarDO> payCarMap = redisCommon.getHashAll(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR, AddPayCarDO.class);
        Iterator<AddPayCarDO> payCarIterator = payCarMap.values().iterator();
        List<PayCar> payCarsList = new ArrayList<>();
        while (payCarIterator.hasNext()) {
            AddPayCarDO next = payCarIterator.next();
            PayCar payCar = new PayCar();
            PayCar car = BeanCopierUtil.copyObj(next, payCar);
            //将购物车数据加入到集合中
            payCarsList.add(car);
        }
        //购物车集合
        order.setPayCar(payCarsList);
        //地址栏接口 是否有
        //优惠卷接口
        //价格计算
        //订单状态
        //商品数量
        //订单号 时间戳+小米id
        //收货地址
        //设置未支付
        order.setState("waitPay");

        //存入redis
        rabbitTemplate.convertAndSend("orderDelayQueue", order, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                System.out.println("创建" + order);
                //设置订单存活时间
                message.getMessageProperties().setExpiration("5000");
                return message;
            }
        });
        return new RestResult<>();
    }
}
